<?php

namespace App\Admin\Controllers;

use App\Miya\Models\Classify;
use App\Miya\Models\Info;
use App\Miya\Models\Keyword;
use App\Miya\Models\Module;
use App\Miya\Tools\ExtendTool;
use App\Miya\Tools\TagTool;
use App\Miya\Tools\TqlTool;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Tree;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\App;
use Illuminate\Validation\Rule;

class ClassifyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '模块分类';

    public function __construct()
    {
        if (!App::runningInConsole()) {
            $module = Module::find(request()->route('module'));
            $this->title = sprintf('%s 分类', $module->name);
        }
    }

    public function index(Content $content)
    {
        $tree = new Tree(new Classify());
        $tree->branch(function ($branch) {
            return "{$branch['id']} - {$branch['name']} tag : {$branch['tag']}";
        });
        $tree->query(function ($model) {
            return $model->where('module_id', request()->route('module'));
        });

        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body($tree);
    }


    public function edit($id, Content $content)
    {
        return parent::edit(request()->route('classify'), $content);
    }

    public function update($id)
    {
        return $this->form()->update(request()->route('classify'));
    }

    public function destroy($id)
    {
        $classify_id = request()->route('classify');

        return $this->form()->destroy($classify_id);
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Classify());

        $module = Module::find(request()->route('module'));

        if ($form->isCreating()) {
            $form->hidden('module_id')->value(request()->route('module'));
        }
        $form->tab('基础设置', function (Form $form) {
            $form->text('name', '分类名称')->rules(['required']);
            $form->select('parent_id', '父分类')
                ->options(Classify::selectOptions(function ($query) {
                    return $query->where('module_id', request()->route('module'));
                }, '根分类'))->rules(['required']);
            $form->image('avatar', '封面图片')->uniqueName()->move(getMovePath('classifies'));
        })->tab('高级设置', function (Form $form) use ($module) {
            if ($form->isCreating()) {
                $form->text('tag', '标签')->rules([Rule::unique('classifies')]);
            } elseif ($form->isEditing()) {
                $classify = Classify::find(request()->route('classify'));
                $form->text('tag', '标签')->rules([Rule::unique('classifies')->ignore($classify)]);
            }
            $form->select('tql', '分类模版')
                ->default($module->classify_tql)->rules(['required'])
                ->options(TqlTool::buildTqlOptions('tql', 'classifies'));
            $form->select('wap_tql', '移动端分类模版')
                ->default($module->wap_classify_tql)->rules(['required'])
                ->options(TqlTool::buildTqlOptions('tql', 'classifies', 'wap'));
        })->tab('页面设置', function (Form $form) {
            $form->text('title', '标题');
            $keywords = Keyword::pluck('keyword', 'id');
            $form->select('keyword_id', '关键词')
                ->options($keywords)
                ->default(app('admin_keyword')->autoSelect('classifies')->id);
            $form->textarea('description', '页面描述');
            $form->UEditor('content', '页面内容');
        })->tab('拓展设置', function (Form $form) use ($module) {
            $form->embeds('extends', '分类拓展信息', function (Form\EmbeddedForm $form) use ($module) {
                ExtendTool::buildForm($form, $module->classify_extends);
            });
        });

        $form->saving(function (Form $form) {
            if (!$form->tag) {
                $form->tag = TagTool::makeTag($form->name, config('miya.tag_suffix.classify'));
            }

            //关键词被删除情况下,重新设置关键词
            $keyword = Keyword::find($form->keyword_id);
            if (!$keyword) {
                $form->keyword_id = app('admin_keyword')->autoSelect('classifies')->id;
            }
        });

        return $form;
    }
}
